package com.jiuye.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jiuye.common.BaseContext;
import com.jiuye.common.CustomException;
import com.jiuye.common.GlobalConstant;
import com.jiuye.common.R;
import com.jiuye.entity.ShoppingCart;
import com.jiuye.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车表示层
 *
 * @author likeWen
 * @since 2022/11/7 11:51
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private IShoppingCartService shoppingCartService;

    /**
     * 添加购物车信息
     *
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCartParam) {
        // 1）查询当前用户的购物车中是否有此菜品或套餐的数据
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //条件一：当前登录用户的ID
        //因为对于C端来说，用户应该只能看到自己的信息
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentUserId());
        // 2）判断当前购物车是否存在此菜品或者套餐.
        //基于数据库的表设计，要么dishID有值，要么setmealId有值，两个字段互相排斥
        //所以这里可以根据前端传入的值来判断，要新增的数据是菜品还是套餐；
        if (shoppingCartParam.getDishId() != null) {
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCartParam.getDishId());
        } else if (shoppingCartParam.getSetmealId() != null) {
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCartParam.getSetmealId());
        } else {
            throw new CustomException("传递的参数不正确");
        }
        ShoppingCart shoppingCart = this.shoppingCartService.getOne(queryWrapper);
        if (shoppingCart != null) {
            // 3）如果存在,就在原来的数量基础上加一
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            // 3.1）更新数据
            this.shoppingCartService.updateById(shoppingCart);
        } else {
            // 4）如果不存在,则添加到购物车,默认数量就是一
            shoppingCart = new ShoppingCart();
            //不存在的商品，添加到购物车，实际上就是在购物车得到表中新增一条数据
            //拿到前端传入的购物车数据
            BeanUtils.copyProperties(shoppingCartParam, shoppingCart);
            // 4.1）在threadlocal中取到当前用户的id,赋值给要添加的购物车数据.及当前时间。
            //当前用户id
            shoppingCart.setUserId(BaseContext.getCurrentUserId());
            //购物车数量
            shoppingCart.setNumber(1);
            //创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 4.2） 新增数据
            this.shoppingCartService.save(shoppingCart);
        }
        return R.success(shoppingCart);
    }

    /**
     * 查询购物车列表
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> getList() {
        //当前用户
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //当前用户的购物车
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentUserId());
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = this.shoppingCartService.list(queryWrapper);
        return R.success(list);
    }
    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        //根据用户id清空购物车
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentUserId());
        //调用service的remove方法
        this.shoppingCartService.remove(queryWrapper);
        return R.success(GlobalConstant.SUCCESS);
    }
    /**
     * 购物车减号实现
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        if (shoppingCart.getSetmealId() == null && shoppingCart.getDishId()==null){
            throw new CustomException(GlobalConstant.FAILED);
        }
        return R.success(this.shoppingCartService.subShoppingCart(shoppingCart));
    }
    
}
